package ee.ttu.agent.smartHouse.services;

import java.util.Date;

import javax.swing.SpringLayout.Constraints;

import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import ee.ttu.agent.smartHouse.agent.FacilitatorAgent;
import ee.ttu.agent.smartHouse.agent.CustomLoggingAgent;
import ee.ttu.agent.smartHouse.util.Constants;
import ee.ttu.agent.smartHouse.util.CustomACLMessage;
import ee.ttu.agent.smartHouse.util.MessageTemplateBuilder;

public class FacilitatorAuthorizationCheckRequestBehaviour extends
		CyclicBehaviour {
	private static final long serialVersionUID = -7193528552785527280L;
	private FacilitatorAgent facilitatorAgent;
	private CustomLoggingAgent loggingService;

	public FacilitatorAuthorizationCheckRequestBehaviour(
			FacilitatorAgent facilitatorAgent) {
		super(facilitatorAgent);
		this.facilitatorAgent = facilitatorAgent;
	}

	public void sendLoggingMessage(String logContent){
		ACLMessage message = new ACLMessage(CustomACLMessage.GATEKEEPER_LOG1);
		message.addReceiver(new AID("GateKeeperLoggingService", AID.ISLOCALNAME));
		message.setOntology(Constants.AUTHORIZATION_CHECK_REQUEST);
		
		message.setSender(this.facilitatorAgent.getAID());
		message.setContent(logContent);
		facilitatorAgent.send(message);
	} 
	
	@Override
	public void action() {
		MessageTemplate authRequestTemplate = MessageTemplateBuilder
				.buildTemplate(ACLMessage.REQUEST,
						Constants.AUTHORIZATION_CHECK_REQUEST);
		MessageTemplate tm2 = MessageTemplateBuilder.buildTemplate(
				CustomACLMessage.TO_USER, Constants.OI_MESSAGES);
		MessageTemplate tm3 = MessageTemplateBuilder.buildTemplate(
				CustomACLMessage.GATEKEEPER_OI_START, Constants.OI_MESSAGES);

		MessageTemplate onlineInteractionTemplate = MessageTemplate
				.or(tm2, tm3);

		ACLMessage msg = facilitatorAgent.receive(MessageTemplate.or(
				authRequestTemplate, onlineInteractionTemplate));
		if (msg != null) {

			if (msg.getOntology().equals(Constants.AUTHORIZATION_CHECK_REQUEST)) {
				ACLMessage replay = new ACLMessage(ACLMessage.INFORM);
				replay.addReceiver(msg.getSender());
				replay.setOntology(Constants.AUTHORIZATION_CHECK_RESPONSE);
				String request = msg.getContent();
				
				if (facilitatorAgent.getRegisteredUsers().containsKey(request))
					replay.setContent(Constants.ALLOW);
					
				else
					replay.setContent(Constants.DENY);
					
				
				facilitatorAgent.send(replay);
				
				//logging entlry
				Date d = new Date(System.currentTimeMillis());
				String date = d.toLocaleString(); 
				String custlogEntry = "Entry attempt from user: ";
				
				custlogEntry += request + " at " + date;
				
				sendLoggingMessage(custlogEntry);
			}

			if (msg.getOntology().equals(Constants.OI_MESSAGES)) {
				if (msg.getPerformative() == CustomACLMessage.GATEKEEPER_OI_START) {
					facilitatorAgent.acceptOIRequest();
				}

				if (msg.getPerformative() == CustomACLMessage.TO_USER) {
					String receivedMessage = msg.getContent();
					facilitatorAgent.setMessage(receivedMessage);
				}
			}
		} else {
			block();
		}
	}
}
